import Cookies from 'js-cookie'
import cache from '@/plugins/cache'

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false,
    hide: false
  },
  device: 'desktop',
  refresh: null, // 更新
  srceenDelay: cache.local.get("srceen-delay") || 10, // 大屏刷新时间 单位秒
  workshopDelay: cache.local.get("workshop-delay") || 10, // 车间轮播时间 单位秒
  workshopArrow: cache.local.get("workshop-arrow") || 'never', // 大屏轮播按钮 always始终显示 hover悬浮显示 never不显示
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    if (state.sidebar.hide) {
      return false;
    }
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_SIDEBAR_HIDE: (state, status) => {
    state.sidebar.hide = status
  },
  // 大屏更新
  SET_REFRESH(state, refresh) {
    state.refresh = refresh;
  },
  // 大屏更新间隔
  SET_SRCEEN_DELAY(state, delay) {
    state.srceenDelay = delay;
  },
  // 车间更新间隔
  SET_WORKSHOP_DELAY(state, delay) {
    state.workshopDelay = delay;
  },
  // 车间切换按钮
  SET_WORKSHOP_ARROW(state, arrow) {
    state.workshopArrow = arrow;
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  toggleSideBarHide({ commit }, status) {
    commit('SET_SIDEBAR_HIDE', status)
  },
  // 设置大屏更新
  setRefresh({ commit }, refresh) {
    commit("SET_REFRESH", refresh);
  },
  // 设置大屏更新间隔
  setSrceenDelay({ commit }, delay) {
    commit("SET_SRCEEN_DELAY", delay);
  },
  // 设置车间更新间隔
  setWorkshopDelay({ commit }, delay) {
    commit("SET_WORKSHOP_DELAY", delay);
  },
  // 设置车间切换按钮
  setWorkshopArrow({ commit }, arrow) {
    commit("SET_WORKSHOP_ARROW", arrow);
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
